#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

typedef struct WORD
{
    char worddata[50];
    int freq;
    struct WORD *link;
} word;
typedef word *wlist;

int num = 0;
char a_word[500][20];

void get_word(char str[]) {
    char *p = str;
    while (*p != '\0') {
        if (isalpha(*p)) {
            int t = 0;
            while (isalpha(*p)) {
                if (isupper(*p)) {
                    a_word[num][t++] = tolower(*p);
                    p++;
                } else {
                    a_word[num][t++] = *p;
                    p++;
                }
            }
            num++;
        }
        if (*p == '\0')
            break;
        p++;
    }
}

int main() {
    FILE *fp1 = fopen("article.txt", "r");
    char sourcestr[800];
    while (fgets(sourcestr, 800, fp1) != NULL)
        get_word(sourcestr);
    wlist w = (wlist) malloc(sizeof(word));//ʽ ĸ
    wlist now;
    int flag = 0;
    int i;
    for (i = 0; i < num; i++) {
        if (flag == 0) {
            flag = 1;
            strcpy(w->worddata, a_word[i]);
            w->freq = 1;
            w->link = NULL;
        } else {
            int havefound = 0;
            wlist p = w;
            while (p != NULL) {
                if (strcmp(p->worddata, a_word[i]) == 0) {
                    (p->freq)++;
                    havefound = 1;
                    break;
                } else if (strcmp(p->worddata, a_word[i]) > 0) {
                    //printf("%s", a_word);
                    havefound = 2;
                    break;
                }

                p = p->link;
            }
            if (havefound == 2) {
                wlist q = w;
                wlist newword = (wlist) malloc(sizeof(word));
                if (p == w)//ͷڵ
                {
                    newword->freq = 1;
                    strcpy(newword->worddata, a_word[i]);
                    newword->link = w;
                    w = newword;
                } else//м
                {
                    while (q->link != p)
                        q = q->link;
                    newword->link = q->link;
                    strcpy(newword->worddata, a_word[i]);
                    newword->freq = 1;
                    q->link = newword;
                }
            } else if (havefound == 0)//
            {
                now = (wlist) malloc(sizeof(word));
                p = w;
                while (p->link != NULL)
                    p = p->link;
                p->link = now;
                now->freq = 1;
                strcpy(now->worddata, a_word[i]);
                now->link = NULL;
            }

        }
    }
    wlist p = w;
    while (p != NULL) {
        if (p->link == NULL)
            printf("%s %d", p->worddata, p->freq);
        else
            printf("%s %d\n", p->worddata, p->freq);
        p = p->link;
    }
    fclose(fp1);
    return 0;
}


/*填空1
 * struct node {
    node *next;
    int num;
};

int main() {
    node *head = (node *) malloc(sizeof(node));
    head->next = head;
    head->num = 20;
    for (int i = 19; i >= 1; i--) {
        node *a = (node *) malloc(sizeof(node));
        a->next = head;
        a->num = i;
        head->next = a;
    }
    int cnt = 1;
    node *first = head->next;
    while (first->next != first) {
        if (cnt == 1) {
            first->next = first->next->next;
            cnt = 0;
        }
        cnt++;
        first = first->next;
    }
    cout << first->num;
}*/
